package cn.woniu.ecommerceplatform.service.impl;

import cn.woniu.ecommerceplatform.util.ResponseResult;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.woniu.ecommerceplatform.entity.Type;
import cn.woniu.ecommerceplatform.service.TypeService;
import cn.woniu.ecommerceplatform.mapper.TypeMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
* @author admin
* @description 针对表【t_type】的数据库操作Service实现
* @createDate 2023-01-05 15:39:28
*/
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type>
    implements TypeService{
    @Autowired(required = false)
    private TypeMapper typeMapper;

    @Override
    public ResponseResult<?> queryList(String typeName,String typeKey,Integer pageNum,Integer pageSize) {
        List<Type> typeList;
        PageInfo<Type> typePageInfo;
        if(pageNum!=null && pageSize!=null){
            PageHelper.startPage(pageNum,pageSize);
            typeList = typeMapper.queryList(typeName, typeKey);
            typePageInfo = new PageInfo<>(typeList);
            if (!(typePageInfo.getSize()>0)&&pageNum>1){
                PageHelper.startPage(pageNum-1,pageSize);
                typePageInfo = new PageInfo<>(typeList);
            }
        } else {
            typeList = typeMapper.queryList(typeName, typeKey);
            typePageInfo = new PageInfo<>(typeList);
        }

        if(typeList.isEmpty()){
            return new ResponseResult<>().isNull();
        }
        return new ResponseResult<>().ok(typePageInfo);
    }

    @Override
    public ResponseResult<?> delTypeById(Integer id) {
        int i = typeMapper.deleteById(id);
        if (i>0){
            return new ResponseResult<>().ok();
        } else {
            return new ResponseResult<>().notok();
        }

    }

    @Override
    public ResponseResult<?> deleteTypes(Integer[] ids) {
        int i = typeMapper.deleteBatchIds(Arrays.asList(ids));
        if (i>0){
            return new ResponseResult<>().ok();
        } else {
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> insertType(Type type) {
        int insert = typeMapper.insert(type);
        if (insert>0){
            return new ResponseResult<>().ok();
        } else {
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> updateTypeById(Type type) {
        int i = typeMapper.updateById(type);
        if (i>0){
            return new ResponseResult<>().ok();
        } else {
            return new ResponseResult<>().notok();
        }
    }
}




